package com.snail.billing.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.snail.billing.json.JsonWorkQuery;

public class WorkDataManager {

	public static final String TEXT = "text";
	public static final String TEXT2 = "text2";

	private static WorkDataManager instance;

	private WorkDataManager() {
	}

	public static final WorkDataManager getInstance() {
		if (instance == null) {
			instance = new WorkDataManager();
		}
		return instance;
	}

	private List<JsonWorkQuery.Problem> listAll = new ArrayList<JsonWorkQuery.Problem>();
	private List<JsonWorkQuery.Problem> listNo = new ArrayList<JsonWorkQuery.Problem>();
	private List<JsonWorkQuery.Problem> listYes = new ArrayList<JsonWorkQuery.Problem>();

	private List<Map<String, String>> dataAll = new ArrayList<Map<String, String>>();
	private List<Map<String, String>> dataNo = new ArrayList<Map<String, String>>();
	private List<Map<String, String>> dataYes = new ArrayList<Map<String, String>>();

	private int countAll;
	private int countNo;
	private int countYes;

	public void add(int type, JsonWorkQuery jsonWorkQuery) {
		List<JsonWorkQuery.Problem> list;
		List<Map<String, String>> data;
		if (type == 0) {
			list = listAll;
			data = dataAll;
			countAll = jsonWorkQuery.getTotal();
		} else if (type == 1) {
			list = listNo;
			data = dataNo;
			countNo = jsonWorkQuery.getTotal();
		} else if (type == 2) {
			list = listYes;
			data = dataYes;
			countYes = jsonWorkQuery.getTotal();
		} else {
			return;
		}
		list.addAll(jsonWorkQuery.getProblems());
		for (JsonWorkQuery.Problem problem : jsonWorkQuery.getProblems()) {
			data.add(buildMap(problem));
		}
	}

	public void create(JsonWorkQuery.Problem problem) {
		Map<String, String> map = buildMap(problem);
		listAll.add(0, problem);
		dataAll.add(0, map);
		countAll++;
		listNo.add(0, problem);
		dataNo.add(0, map);
		countNo++;
	}

	public void delete(JsonWorkQuery.Problem problem) {
		for (int i = 0; i < listAll.size(); i++) {
			JsonWorkQuery.Problem tempProblem = listAll.get(i);
			if (tempProblem.getId().equals(problem.getId())) {
				listAll.remove(i);
				dataAll.remove(i);
				countAll--;
				break;
			}
		}

		for (int i = 0; i < listNo.size(); i++) {
			JsonWorkQuery.Problem tempProblem = listNo.get(i);
			if (tempProblem.getId().equals(problem.getId())) {
				listNo.remove(i);
				dataNo.remove(i);
				countNo--;
				break;
			}
		}

		for (int i = 0; i < listYes.size(); i++) {
			JsonWorkQuery.Problem tempProblem = listYes.get(i);
			if (tempProblem.getId().equals(problem.getId())) {
				listYes.remove(i);
				dataYes.remove(i);
				countYes--;
				break;
			}
		}

	}

	public void clear() {
		listAll.clear();
		listNo.clear();
		listYes.clear();
		dataAll.clear();
		dataNo.clear();
		dataYes.clear();
		countAll = 0;
		countNo = 0;
		countYes = 0;
	}

	public Map<String, String> buildMap(JsonWorkQuery.Problem problem) {
		Map<String, String> map = new HashMap<String, String>();
		if (problem == null) {
			map.put(TEXT, "");
			map.put(TEXT2, "");
		} else {
			map.put(TEXT, problem.getTitle());
			map.put(TEXT2, "提问时间：" + problem.getCreateDate());
		}
		return map;
	}

	public List<JsonWorkQuery.Problem> getList(int type) {
		if (type == 0) {
			return listAll;
		} else if (type == 1) {
			return listNo;
		} else if (type == 2) {
			return listYes;
		}
		return null;
	}

	public List<JsonWorkQuery.Problem> getListAll() {
		return listAll;
	}

	public List<JsonWorkQuery.Problem> getListNo() {
		return listNo;
	}

	public List<JsonWorkQuery.Problem> getListYes() {
		return listYes;
	}

	public List<Map<String, String>> getData(int type) {
		if (type == 0) {
			return dataAll;
		} else if (type == 1) {
			return dataNo;
		} else if (type == 2) {
			return dataYes;
		}
		return null;
	}

	public List<Map<String, String>> getDataAll() {
		return dataAll;
	}

	public List<Map<String, String>> getDataNo() {
		return dataNo;
	}

	public List<Map<String, String>> getDataYes() {
		return dataYes;
	}

	public int getCount(int type) {
		if (type == 0) {
			return countAll;
		} else if (type == 1) {
			return countNo;
		} else if (type == 2) {
			return countYes;
		}
		return 0;
	}

	public int getCountAll() {
		return countAll;
	}

	public int getCountNo() {
		return countNo;
	}

	public int getCountYes() {
		return countYes;
	}

	public boolean isEmpty() {
		return listAll.isEmpty();
	}

}
